package com.baizhi.zyj.service;

import com.baizhi.zyj.dao.UserMapper;
import com.baizhi.zyj.entity.User;
import com.baizhi.zyj.entity.UserExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryAllPage(int page, int pageSize) {
        User user = new User();
        UserExample userExample = new UserExample();
        RowBounds rowBounds = new RowBounds((page-1)*pageSize, pageSize);
        // 查所有的数据分页
        List<User> users = userDao.selectByRowBounds(user, rowBounds);
        int totalCount = userDao.selectCount(null);
        Map<String,Object> map = new HashMap<>();
        map.put("total",totalCount);
        //System.out.println(totalCount);
        map.put("page",page);
       // System.out.println(page);
        map.put("rows",users);
        //System.out.println(users);
        return map;
    }


    //修改状态
    @Override
    public Map<String, Object> update(User user) {
        Map<String,Object> map = new HashMap<>();
        try {
            userDao.updateByPrimaryKeySelective(user);
            map.put("message","操作成功");
            map.put("stauts",200);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","操作失败");
            map.put("stauts",400);
            return map;
        }
    }
}
